﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using gfLibrary;
using DBUtility;
using Model;
using BLL;

namespace GreatGF
{
    public partial class CONST_DEFINE_Insert_Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {

                string strMode = Request["m"];

                if((strMode == "u")||(strMode == "b")||(strMode == "r"))
                {
                    string strId = Request["UniqueKey"];

                    CONST_DEFINE_BLL m_CONST_DEFINE_BLL = new CONST_DEFINE_BLL();

                    if(m_CONST_DEFINE_BLL.Exists(Convert.ToInt32(strId)) == true)
                    {
                        CONST_DEFINE m_CONST_DEFINE = new CONST_DEFINE();
                        m_CONST_DEFINE = m_CONST_DEFINE_BLL.GetModel(Convert.ToInt32(strId));

                        txtID.Text = m_CONST_DEFINE.ID.ToString();
                        txtCON_CHNAME.Text = m_CONST_DEFINE.CON_CHNAME;
                        txtCON_NAME.Text = m_CONST_DEFINE.CON_NAME;
                        txtCON_CODE.Text = m_CONST_DEFINE.CON_CODE;
                        txtCON_CON.Text = m_CONST_DEFINE.CON_CON;
                        txtCON_SPELL.Text = m_CONST_DEFINE.CON_SPELL;
                        txtCON_ORDER.Text = m_CONST_DEFINE.CON_ORDER.ToString();
                        txtCON_FLAG.Text = m_CONST_DEFINE.CON_FLAG.ToString();
                        txtCON_VTYPE.Text = m_CONST_DEFINE.CON_VTYPE.ToString();
                        txtCON_CLASS.Text = m_CONST_DEFINE.CON_CLASS.ToString();
                        txtREMARK.Text = m_CONST_DEFINE.REMARK;
                        txtCON_JCTYPE.Text = m_CONST_DEFINE.CON_JCTYPE;
                    }
                    else
                    {
                        gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                        gfAjaxJs.SetGoToUrl(this.UpdatePanel1, "CONST_DEFINE_List.aspx");
                    }

                    if((strMode == "b")||(strMode == "r"))
                    {
                        txtID.ReadOnly = true;
                        txtID.CssClass = "csstextboxreadonly";
                        txtCON_CHNAME.ReadOnly = true;
                        txtCON_CHNAME.CssClass = "csstextboxreadonly";
                        txtCON_NAME.ReadOnly = true;
                        txtCON_NAME.CssClass = "csstextboxreadonly";
                        txtCON_CODE.ReadOnly = true;
                        txtCON_CODE.CssClass = "csstextboxreadonly";
                        txtCON_CON.ReadOnly = true;
                        txtCON_CON.CssClass = "csstextboxreadonly";
                        txtCON_SPELL.ReadOnly = true;
                        txtCON_SPELL.CssClass = "csstextboxreadonly";
                        txtCON_ORDER.ReadOnly = true;
                        txtCON_ORDER.CssClass = "csstextboxreadonly";
                        txtCON_FLAG.ReadOnly = true;
                        txtCON_FLAG.CssClass = "csstextboxreadonly";
                        txtCON_VTYPE.ReadOnly = true;
                        txtCON_VTYPE.CssClass = "csstextboxreadonly";
                        txtCON_CLASS.ReadOnly = true;
                        txtCON_CLASS.CssClass = "csstextboxreadonly";
                        txtREMARK.ReadOnly = true;
                        txtREMARK.CssClass = "csstextboxreadonly";
                        txtCON_JCTYPE.ReadOnly = true;
                        txtCON_JCTYPE.CssClass = "csstextboxreadonly";
                        btnSave.Visible = false;
                    }

                    if(strMode == "r")
                    {
                        btnBack.Visible = false;
                    }
                }
                if (strMode == "i")
                {
                    lblOperateType.Text = "添加";
                }
                if (strMode == "b" || strMode == "r")
                {
                    lblOperateType.Text = "查看";
                }
                if (strMode == "u")
                {
                    lblOperateType.Text = "修改";
                }
            }

            btnBack.Attributes["onclick"] = "javascript:window.location.href='CONST_DEFINE_List.aspx';return false;";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strID = txtID.Text.Trim();
            string strCON_CHNAME = txtCON_CHNAME.Text.Trim();
            string strCON_NAME = txtCON_NAME.Text.Trim();
            string strCON_CODE = txtCON_CODE.Text.Trim();
            string strCON_CON = txtCON_CON.Text.Trim();
            string strCON_SPELL = txtCON_SPELL.Text.Trim();
            string strCON_ORDER = txtCON_ORDER.Text.Trim();
            string strCON_FLAG = txtCON_FLAG.Text.Trim();
            string strCON_VTYPE = txtCON_VTYPE.Text.Trim();
            string strCON_CLASS = txtCON_CLASS.Text.Trim();
            string strREMARK = txtREMARK.Text.Trim();
            string strCON_JCTYPE = txtCON_JCTYPE.Text.Trim();

            if (gfValidate.Is_Integer(strCON_ORDER) == false)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "[CON_ORDER]不是有效的数字格式。");
                return;
            }
            if (gfValidate.Is_Integer(strCON_FLAG) == false)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "[CON_FLAG]不是有效的数字格式。");
                return;
            }
            if (gfValidate.Is_Integer(strCON_VTYPE) == false)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "[CON_VTYPE]不是有效的数字格式。");
                return;
            }
            if (gfValidate.Is_Integer(strCON_CLASS) == false)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "[CON_CLASS]不是有效的数字格式。");
                return;
            }

            CONST_DEFINE m_CONST_DEFINE = new CONST_DEFINE();
            CONST_DEFINE_BLL m_CONST_DEFINE_BLL = new CONST_DEFINE_BLL();

            m_CONST_DEFINE.CON_CHNAME = strCON_CHNAME;
            m_CONST_DEFINE.CON_NAME = strCON_NAME;
            m_CONST_DEFINE.CON_CODE = strCON_CODE;
            m_CONST_DEFINE.CON_CON = strCON_CON;
            m_CONST_DEFINE.CON_SPELL = strCON_SPELL;
            m_CONST_DEFINE.CON_ORDER = Convert.ToInt32(strCON_ORDER);
            m_CONST_DEFINE.CON_FLAG = Convert.ToInt32(strCON_FLAG);
            m_CONST_DEFINE.CON_VTYPE = Convert.ToInt32(strCON_VTYPE);
            m_CONST_DEFINE.CON_CLASS = Convert.ToInt32(strCON_CLASS);
            m_CONST_DEFINE.REMARK = strREMARK;
            m_CONST_DEFINE.CON_JCTYPE = strCON_JCTYPE;

            if (string.IsNullOrEmpty(strID) == true)
            {
                strID = "-1";
            }

            if (m_CONST_DEFINE_BLL.Exists(Convert.ToInt32(strID)) == false)
            {
                //m_CONST_DEFINE.ID = m_CONST_DEFINE_BLL.GetMax_ID();

                if (m_CONST_DEFINE_BLL.Insert(m_CONST_DEFINE) == true)
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "新增成功！");
                    txtID.Text = m_CONST_DEFINE.ID.ToString();
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "新增失败！");
                }
            }
            else
            {
                if (m_CONST_DEFINE_BLL.Update(m_CONST_DEFINE, Convert.ToInt32(strID)) == true)
                {
                     gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "更新成功！");
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "更新失败！");
                }
            }
        }
    }
}
